package com.koron.css2.newProjectCenter.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjPubUpMaterial;
import com.koron.css2.newProjectCenter.bean.query.PjPubUpMaterialQuery;
import com.koron.css2.newProjectCenter.mapper.PjPubUpMaterialMapper;
import com.koron.css2.newProjectCenter.mapper.PubUpStoreMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * @Author: 苏小龙
 * @CreateTime: 2024-12-04
 * @Description: 材料信息
 * @Version: 1.0
 */
public class PjPubUpMaterialList implements ServerInterface {
    /**
     * 执行材料信息的分页查询操作
     *
     * @param factory  数据库会话工厂，用于创建数据库会话
     * @param userInfo 用户信息，包含当前操作用户的相关信息
     * @param req      请求信息，包含分页查询的请求参数
     * @return 返回一个MessageBean对象，包含查询结果或错误信息
     */
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            // 将请求数据转换为材料信息查询对象
            PjPubUpMaterialQuery pubUpInvoiceQuery = JsonUtils.objectToPojo(req.getData(), PjPubUpMaterialQuery.class);
            // 获取材料信息映射器实例
            PjPubUpMaterialMapper pjPubUpInvoiceMapper = factory.getMapper(PjPubUpMaterialMapper.class);

            if (pubUpInvoiceQuery.getToDo() != null && pubUpInvoiceQuery.getToDo() == 4) {
                if (pubUpInvoiceQuery.getId() == null)
                    return MessageBean.create(Constant.MESSAGE_INT_FAIL, "参数错误", null);
                // 根据材料id，获取材料详情
                PjPubUpMaterial material = pjPubUpInvoiceMapper.getMaterialById(pubUpInvoiceQuery.getId());
                if (material == null)
                    return MessageBean.create(Constant.MESSAGE_INT_FAIL, "材料信息不存在", null);
                MessageBean<PjPubUpMaterial> bean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "材料信息查询成功", PjPubUpMaterial.class);
                bean.setData(material);
                return bean;
            }

            PubUpStoreMapper pubUpStoreMapper = factory.getMapper(PubUpStoreMapper.class);
            // 启用MyBatis分页插件，设置当前页和每页记录数
            PageHelper.startPage(pubUpInvoiceQuery.getPage(), pubUpInvoiceQuery.getPageCount());

            // 执行分页查询，获取材料信息列表
            List<PjPubUpMaterial> pubUpInvoiceList = pjPubUpInvoiceMapper.getList(pubUpInvoiceQuery);

            pubUpInvoiceList.forEach(item -> {
                String manufacturerName = pjPubUpInvoiceMapper.getNameById(item.getManufacturer());
                item.setManufacturer(manufacturerName);
                String supplier = pjPubUpInvoiceMapper.getNameById(item.getSupplier());
                item.setSupplier(supplier);
                String materialName = pjPubUpInvoiceMapper.getMaterialNameById(item.getDefaultStore());
                item.setDefaultStore(materialName);
                Integer totalQuantity = pjPubUpInvoiceMapper.getTotalQuantityByMaterialId(item.getId());
                item.setStockQuantity(totalQuantity);
                if (pubUpInvoiceQuery.getStoreId() != null)
                    item.setQuantity(pubUpStoreMapper.getQuantityByStoreIdAndMaterialId(pubUpInvoiceQuery.getStoreId(), item.getId()));
                // 获取仓库列表
                item.setStoreList(pjPubUpInvoiceMapper.getStoreList(item.getId()));
            });

            // 创建成功消息对象，用于返回查询结果
            MessageBean<PageInfo> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "材料信息分页查询成功", PageInfo.class);
            messageBean.setData(new PageInfo<>(pubUpInvoiceList));

            // 返回成功消息和查询结果
            return messageBean;
        } catch (Exception e) {
            // 记录错误日志
            logger.error("材料信息分页查询失败", e);

            // 返回失败消息
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "材料信息分页查询失败:", null);
        }
    }
}
